# utils/verify.py
"""
验证码功能实现
"""

import random
from dao.redis_connect import RedisClient

# 获取redis客户端实例
redis_client = RedisClient.get_client()


def send_verification_code(phone, code_type, expire_in=300):
    code = str(random.randint(100000, 999999))
    redis_client.setex(phone, expire_in, code)  # 使用传入的过期时间
    print(f"验证码 {code} 发送至 {phone}")
    return code


def verify_code(target, code):
    stored_code = redis_client.get(target)
    return stored_code and stored_code == code  # 由于设置了decode_responses=True，不需要decode()
